<?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?>
    	<?php if(isset($breadcrumb['href']) && $breadcrumb['href'] != '' && $breadcrumb['href'] != '#') {?>
	    	<a href="<?php echo $breadcrumb['href']; ?>">
	    		<?php echo $breadcrumb['text']; ?>
	    	</a>
    	<?php }else{?>
    		<?php echo $breadcrumb['text']; ?>
    	<?php }?>
    <?php } ?>
  </div>
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/food.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a onclick="location = '<?php echo $cancel; ?>';" class="button"><?php echo $button_cancel; ?></a></div>
    </div>
    <div class="content">
      <div id="tabs" class="htabs">
      	<a href="#tab-general"><?php echo $tab_general; ?></a>
      	<a href="#tab-data"><?php echo $tab_data; ?></a>
      	<a href="#tab-attribute"><?php echo $tab_attribute; ?></a>
      	<a href="#tab-option"><?php echo $tab_option; ?></a>
      	<a href="#tab-special"><?php echo $tab_special; ?></a>
      	<a href="#tab-image"><?php echo $tab_image; ?></a>
      </div>
      <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
        <div id="tab-general">
          <div id="languages" class="htabs">
            <?php foreach ($languages as $language) { ?>
            <a href="#language<?php echo $language['language_id']; ?>"><img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>" /> <?php echo $language['name']; ?></a>
            <?php } ?>
          </div>
          <?php foreach ($languages as $language) { ?>
          <div id="language<?php echo $language['language_id']; ?>">
            <table class="form">
              <tr>
                <td>
	                <span class="required">*</span>
                	<?php echo $entry_name; ?></td>
                <td><input type="text" name="food_description[<?php echo $language['language_id']; ?>][name]" size="100" value="<?php echo isset($food_description[$language['language_id']]) ? $food_description[$language['language_id']]['name'] : ''; ?>" />
                  <?php if (isset($error_name[$language['language_id']])) { ?>
                  <span class="error"><?php echo $error_name[$language['language_id']]; ?></span>
                  <?php } ?></td>
              </tr>
              <tr>
                <td><?php echo $entry_meta_description; ?></td>
                <td><textarea name="food_description[<?php echo $language['language_id']; ?>][meta_description]" cols="40" rows="5"><?php echo isset($food_description[$language['language_id']]) ? $food_description[$language['language_id']]['meta_description'] : ''; ?></textarea></td>
              </tr>
              <tr>
                <td><?php echo $entry_meta_keyword; ?></td>
                <td><textarea name="food_description[<?php echo $language['language_id']; ?>][meta_keyword]" cols="40" rows="5"><?php echo isset($food_description[$language['language_id']]) ? $food_description[$language['language_id']]['meta_keyword'] : ''; ?></textarea></td>
              </tr>
              <tr>
                <td><?php echo $entry_description; ?></td>
                <td><textarea name="food_description[<?php echo $language['language_id']; ?>][description]" id="description<?php echo $language['language_id']; ?>"><?php echo isset($food_description[$language['language_id']]) ? $food_description[$language['language_id']]['description'] : ''; ?></textarea></td>
              </tr>
              <tr>
                <td><?php echo $entry_tag; ?></td>
                <td><input type="text" name="food_tag[<?php echo $language['language_id']; ?>]" value="<?php echo isset($food_tag[$language['language_id']]) ? $food_tag[$language['language_id']] : ''; ?>" size="80" /></td>
              </tr>
            </table>
          </div>
          <?php } ?>
        </div>
        <div id="tab-data">
          <table class="form">
            <tr>
              <td><span class="required">*</span> <?php echo $entry_menu; ?></td>
              <td>
	            <div class="scrollbox" style="height:200px">
                  <?php $class = 'odd'; ?>
                  <?php foreach ($menus as $menu) { ?>
                  <?php $class = ($class == 'even' ? 'odd' : 'even'); ?>
                  <div class="<?php echo $class; ?>">
                    <?php if (in_array($menu['menu_id'], $food_menu)) { ?>
                    <input type="checkbox" name="food_menu[]" value="<?php echo $menu['menu_id']; ?>" checked="checked" />
                    <?php echo $menu['name']; ?>
                    <?php } else { ?>
                    <input type="checkbox" name="food_menu[]" value="<?php echo $menu['menu_id']; ?>" />
                    <?php echo $menu['name']; ?>
                    <?php } ?>
                  </div>
                  <?php } ?>
                </div>
                <a onclick="$(this).parent().find(':checkbox').attr('checked', true);"><?php echo $text_select_all; ?></a> / <a onclick="$(this).parent().find(':checkbox').attr('checked', false);"><?php echo $text_unselect_all; ?></a>
                <?php if (isset($error_menu)) { ?>
                <span class="error"><?php echo $error_menu; ?></span>
                <?php } ?>
              </td>
            </tr>
            <tr>
              <td><?php echo $entry_price; ?></td>
              <td><input type="text" name="price" value="<?php echo $price; ?>" /></td>
            </tr>
            <tr>
              <td><?php echo $entry_shipping; ?></td>
              <td><?php if ($shipping) { ?>
                <input type="radio" name="shipping" value="1" checked="checked" />
                <?php echo $text_yes; ?>
                <input type="radio" name="shipping" value="0" />
                <?php echo $text_no; ?>
                <?php } else { ?>
                <input type="radio" name="shipping" value="1" />
                <?php echo $text_yes; ?>
                <input type="radio" name="shipping" value="0" checked="checked" />
                <?php echo $text_no; ?>
                <?php } ?></td>
            </tr>
            <tr>
              <td><?php echo $entry_keyword; ?></td>
              <td><input type="text" name="keyword" value="<?php echo $keyword; ?>" /></td>
            </tr>
            <tr>
              <td><?php echo $entry_image; ?></td>
              <td valign="top"><div class="image"><img src="<?php echo $thumb; ?>" alt="" id="thumb" />
                <input type="hidden" name="image" value="<?php echo $image; ?>" id="foodimage" />
                <br /><a onclick="image_upload('foodimage' , 'food/food', 'thumb');"><?php echo $text_browse; ?></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a onclick="$('#thumb').attr('src', '<?php echo $no_image; ?>'); $('#image').attr('value', '');"><?php echo $text_clear; ?></a></div></td>
            </tr>
            <tr>
              <td><?php echo $entry_top; ?></td>
              <td><?php if ($top) { ?>
                <input type="checkbox" name="top" value="1" checked="checked" />
                <?php } else { ?>
                <input type="checkbox" name="top" value="1" />
                <?php } ?></td>
            </tr>
            <tr>
              <td><?php echo $entry_sort_order; ?></td>
              <td><input type="text" name="sort_order" value="<?php echo $sort_order; ?>" size="1" /></td>
            </tr>
            <tr>
              <td><?php echo $entry_status; ?></td>
              <td><select name="status">
                  <?php if ($status) { ?>
                  <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                  <option value="0"><?php echo $text_disabled; ?></option>
                  <?php } else { ?>
                  <option value="1"><?php echo $text_enabled; ?></option>
                  <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                  <?php } ?>
                </select></td>
            </tr>
          </table>
        </div>
        <div id="tab-attribute">
          <table id="attribute" class="list">
            <thead>
              <tr>
                <td class="left"><?php echo $entry_attribute; ?></td>
                <td class="left" style="width:300px"><?php echo $entry_text; ?></td>
                <td></td>
              </tr>
            </thead>
            <?php $attribute_row = 0; ?>
            <?php foreach ($food_attributes as $food_attribute) { ?>
            <tbody id="attribute-row<?php echo $attribute_row; ?>">
              <tr>
                <td class="left"><input size="50" type="text" name="food_attribute[<?php echo $attribute_row; ?>][name]" value="<?php echo $food_attribute['name']; ?>" />
                  <input type="hidden" name="food_attribute[<?php echo $attribute_row; ?>][attribute_id]" value="<?php echo $food_attribute['attribute_id']; ?>" /></td>
                <td class="left"><?php foreach ($languages as $language) { ?>
                  <textarea name="food_attribute[<?php echo $attribute_row; ?>][food_attribute_description][<?php echo $language['language_id']; ?>][text]" cols="40" rows="5"><?php echo isset($food_attribute['food_attribute_description'][$language['language_id']]) ? $food_attribute['food_attribute_description'][$language['language_id']]['text'] : ''; ?></textarea>
                  <img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>" /><br />
                  <?php } ?></td>
                <td class="center"><a onclick="$('#attribute-row<?php echo $attribute_row; ?>').remove();" class="button"><?php echo $button_remove; ?></a></td>
              </tr>
            </tbody>
            <?php $attribute_row++; ?>
            <?php } ?>
            <tfoot>
              <tr>
                <td colspan="2"></td>
                <td class="center" style="width:150px"><a onclick="addAttribute();" class="button"><?php echo $button_add_attribute; ?></a></td>
              </tr>
            </tfoot>
          </table>
        </div>
        <div id="tab-option">
          <div id="vtab-option" class="vtabs">
            <?php $option_row = 0; ?>
            <?php foreach ($food_options as $food_option) { ?>
            <a href="#tab-option-<?php echo $option_row; ?>" id="option-<?php echo $option_row; ?>"><?php echo $food_option['name']; ?>&nbsp;<img src="view/image/delete.png" alt="" onclick="$('#vtabs a:first').trigger('click'); $('#option-<?php echo $option_row; ?>').remove(); $('#tab-option-<?php echo $option_row; ?>').remove(); return false;" /></a>
            <?php $option_row++; ?>
            <?php } ?>
            <span id="option-add">
            <select name="option">
            	<option value=""><?php echo $text_select;?></option>
            	<?php foreach ($options as $option){?>
            	<option value="<?php echo $option['option_id'] . '_' . $option['type'];?>"><?php echo $option['name'];?></option>
            	<?php }?>
            </select>
            <input type="hidden" id='selected_hidden_food_option_id' value=""/>
            <input type="hidden" id='selected_hidden_food_option_name' value=""/>
            <input type="hidden" id='selected_hidden_food_option_type' value=""/>
            &nbsp;<img src="view/image/add.png" id="button-add-option" alt="<?php echo $button_add_option; ?>" title="<?php echo $button_add_option; ?>" /></span></div>
          <?php $option_row = 0; ?>
          <?php $option_value_row = 0; ?>
          <?php foreach ($food_options as $food_option) { ?>
          <div id="tab-option-<?php echo $option_row; ?>" class="vtabs-content">
            <input type="hidden" name="food_option[<?php echo $option_row; ?>][food_option_id]" value="<?php echo $food_option['food_option_id']; ?>" />
            <input type="hidden" name="food_option[<?php echo $option_row; ?>][name]" value="<?php echo $food_option['name']; ?>" />
            <input type="hidden" name="food_option[<?php echo $option_row; ?>][option_id]" value="<?php echo $food_option['option_id']; ?>" />
            <input type="hidden" name="food_option[<?php echo $option_row; ?>][type]" value="<?php echo $food_option['type']; ?>" />
            <table class="form">
              <tr>
                <td><?php echo $entry_required; ?></td>
                <td><select name="food_option[<?php echo $option_row; ?>][required]">
                    <?php if ($food_option['required']) { ?>
                    <option value="1" selected="selected"><?php echo $text_yes; ?></option>
                    <option value="0"><?php echo $text_no; ?></option>
                    <?php } else { ?>
                    <option value="1"><?php echo $text_yes; ?></option>
                    <option value="0" selected="selected"><?php echo $text_no; ?></option>
                    <?php } ?>
                  </select></td>
              </tr>
              <?php if ($food_option['type'] == 'text') { ?>
              <tr>
                <td><?php echo $entry_option_value; ?></td>
                <td><input type="text" name="food_option[<?php echo $option_row; ?>][option_value]" value="<?php echo $food_option['option_value']; ?>" /></td>
              </tr>
              <?php } ?>
              <?php if ($food_option['type'] == 'textarea') { ?>
              <tr>
                <td><?php echo $entry_option_value; ?></td>
                <td><textarea name="food_option[<?php echo $option_row; ?>][option_value]" cols="40" rows="5"><?php echo $food_option['option_value']; ?></textarea></td>
              </tr>
              <?php } ?>
              <?php if ($food_option['type'] == 'file') { ?>
              <tr style="display: none;">
                <td><?php echo $entry_option_value; ?></td>
                <td><input type="text" name="food_option[<?php echo $option_row; ?>][option_value]" value="<?php echo $food_option['option_value']; ?>" /></td>
              </tr>
              <?php } ?>
              <?php if ($food_option['type'] == 'date') { ?>
              <tr>
                <td><?php echo $entry_option_value; ?></td>
                <td><input type="text" name="food_option[<?php echo $option_row; ?>][option_value]" value="<?php echo $food_option['option_value']; ?>" class="date" /></td>
              </tr>
              <?php } ?>
              <?php if ($food_option['type'] == 'datetime') { ?>
              <tr>
                <td><?php echo $entry_option_value; ?></td>
                <td><input type="text" name="food_option[<?php echo $option_row; ?>][option_value]" value="<?php echo $food_option['option_value']; ?>" class="datetime" /></td>
              </tr>
              <?php } ?>
              <?php if ($food_option['type'] == 'time') { ?>
              <tr>
                <td><?php echo $entry_option_value; ?></td>
                <td><input type="text" name="food_option[<?php echo $option_row; ?>][option_value]" value="<?php echo $food_option['option_value']; ?>" class="time" /></td>
              </tr>
              <?php } ?>
            </table>
            <?php if ($food_option['type'] == 'select' || $food_option['type'] == 'radio' || $food_option['type'] == 'checkbox' || $food_option['type'] == 'image') { ?>
            <table id="option-value<?php echo $option_row; ?>" class="list">
              <thead>
                <tr>
                  <td class="left"><?php echo $entry_option_value; ?></td>
                  <td class="right" style="width:200px"><?php echo $entry_price; ?></td>
                  <td style="width:100px"></td>
                </tr>
              </thead>
              <?php foreach ($food_option['food_option_value'] as $food_option_value) { ?>
              <tbody id="option-value-row<?php echo $option_value_row; ?>">
                <tr>
                  <td class="left"><select name="food_option[<?php echo $option_row; ?>][food_option_value][<?php echo $option_value_row; ?>][option_value_id]">
                    </select>
                    <input type="hidden" name="food_option[<?php echo $option_row; ?>][food_option_value][<?php echo $option_value_row; ?>][food_option_value_id]" value="<?php echo $food_option_value['food_option_value_id']; ?>" /></td>
                  <td class="right" style="width:200px"><select name="food_option[<?php echo $option_row; ?>][food_option_value][<?php echo $option_value_row; ?>][price_prefix]">
                      <?php if ($food_option_value['price_prefix'] == '+') { ?>
                      <option value="+" selected="selected">+</option>
                      <?php } else { ?>
                      <option value="+">+</option>
                      <?php } ?>
                      <?php if ($food_option_value['price_prefix'] == '-') { ?>
                      <option value="-" selected="selected">-</option>
                      <?php } else { ?>
                      <option value="-">-</option>
                      <?php } ?>
                    </select>
                    <input type="text" name="food_option[<?php echo $option_row; ?>][food_option_value][<?php echo $option_value_row; ?>][price]" value="<?php echo $food_option_value['price']; ?>" size="5" /></td>
                  <td class="center" style="width:100px"><a onclick="$('#option-value-row<?php echo $option_value_row; ?>').remove();" class="button"><?php echo $button_remove; ?></a></td>
                </tr>
              </tbody>
              <?php $option_value_row++; ?>
              <?php } ?>
              <tfoot>
                <tr>
                  <td colspan="2"></td>
                  <td class="center"><a onclick="addOptionValue('<?php echo $option_row; ?>');" class="button"><?php echo $button_add_option_value; ?></a></td>
                </tr>
              </tfoot>
            </table>
            <?php } ?>
          </div>
          <?php $option_row++; ?>
          <?php } ?>
          <script type="text/javascript"><!--
          <?php $option_row = 0; ?>
          <?php $option_value_row = 0; ?>		  
		  <?php foreach ($food_options as $food_option) { ?>
          <?php if ($food_option['type'] == 'select' || $food_option['type'] == 'radio' || $food_option['type'] == 'checkbox' || $food_option['type'] == 'image') { ?>
		  <?php foreach ($food_option['food_option_value'] as $food_option_value) { ?>
		  $('select[name=\'food_option[<?php echo $option_row; ?>][food_option_value][<?php echo $option_value_row; ?>][option_value_id]\']').load('index.php?route=menu/autocomplete/foodOptionValue&token=<?php echo $token; ?>&option_id=<?php echo $food_option['option_id']; ?>&option_value_id=<?php echo $food_option_value['option_value_id']; ?>');
		  <?php $option_value_row++; ?>
		  <?php } ?>
		  <?php } ?>
		  <?php $option_row++; ?>
          <?php } ?>
		  //--></script> 
        </div>       
        <div id="tab-special">
          <table id="special" class="list">
            <thead>
              <tr>
                <td class="right" style="width:100px"><?php echo $entry_priority; ?></td>
                <td class="right"><?php echo $entry_price; ?></td>
                <td class="left" style="width:200px"><?php echo $entry_date_start; ?></td>
                <td class="left" style="width:200px"><?php echo $entry_date_end; ?></td>
                <td></td>
              </tr>
            </thead>
            <?php $special_row = 0; ?>
            <?php foreach ($food_specials as $food_special) { ?>
            <tbody id="special-row<?php echo $special_row; ?>">
              <tr>
                <td class="right"><input type="text" name="food_special[<?php echo $special_row; ?>][priority]" value="<?php echo $food_special['priority']; ?>" size="2" /></td>
                <td class="right"><input type="text" name="food_special[<?php echo $special_row; ?>][price]" value="<?php echo $food_special['price']; ?>" /></td>
                <td class="left"><input type="text" name="food_special[<?php echo $special_row; ?>][date_start]" value="<?php echo $food_special['date_start']; ?>" class="date" /></td>
                <td class="left"><input type="text" name="food_special[<?php echo $special_row; ?>][date_end]" value="<?php echo $food_special['date_end']; ?>" class="date" /></td>
                <td class="left"><a onclick="$('#special-row<?php echo $special_row; ?>').remove();" class="button"><?php echo $button_remove; ?></a></td>
              </tr>
            </tbody>
            <?php $special_row++; ?>
            <?php } ?>
            <tfoot>
              <tr>
                <td colspan="4"></td>
                <td class="center" style="width:150px"><a onclick="addSpecial();" class="button"><?php echo $button_add_special; ?></a></td>
              </tr>
            </tfoot>
          </table>
        </div>    
        <div id="tab-image">
          <table id="images" class="list">
            <thead>
              <tr>
                <td class="left" style="width:150px"><?php echo $entry_image; ?></td>
                <td class="right"><?php echo $entry_sort_order; ?></td>
                <td style="width:150px"></td>
              </tr>
            </thead>
            <?php $image_row = 0; ?>
            <?php foreach ($food_images as $food_image) { ?>
            <tbody id="image-row<?php echo $image_row; ?>">
              <tr>
                <td class="left"><div class="image"><img src="<?php echo $food_image['thumb']; ?>" alt="" id="thumb<?php echo $image_row; ?>" />
                    <input type="hidden" name="food_image[<?php echo $image_row; ?>][image]" value="<?php echo $food_image['image']; ?>" id="image<?php echo $image_row; ?>" />
                    <br />
                    <a onclick="image_upload('image<?php echo $image_row; ?>', 'food/food', 'thumb<?php echo $image_row; ?>');"><?php echo $text_browse; ?></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a onclick="$('#thumb<?php echo $image_row; ?>').attr('src', '<?php echo $no_image; ?>'); $('#image<?php echo $image_row; ?>').attr('value', '');"><?php echo $text_clear; ?></a></div></td>
                <td class="right"><input type="text" name="food_image[<?php echo $image_row; ?>][sort_order]" value="<?php echo $food_image['sort_order']; ?>" size="2" /></td>
                <td class="center"><a onclick="$('#image-row<?php echo $image_row; ?>').remove();" class="button"><?php echo $button_remove; ?></a></td>
              </tr>
            </tbody>
            <?php $image_row++; ?>
            <?php } ?>
            <tfoot>
              <tr>
                <td colspan="2"></td>
                <td class="center"><a onclick="addImage();" class="button"><?php echo $button_add_image; ?></a></td>
              </tr>
            </tfoot>
          </table>
        </div>  
      </form>
    </div>
  </div>
</div>
<script type="text/javascript" src="view/javascript/ckeditor/ckeditor.js"></script> 
<script type="text/javascript"><!--
<?php foreach ($languages as $language) { ?>
CKEDITOR.replace('description<?php echo $language['language_id']; ?>', {
	filebrowserBrowseUrl: 'index.php?route=common/filemanager&token=<?php echo $token; ?>',
	filebrowserImageBrowseUrl: 'index.php?route=common/filemanager&token=<?php echo $token; ?>',
	filebrowserFlashBrowseUrl: 'index.php?route=common/filemanager&token=<?php echo $token; ?>',
	filebrowserUploadUrl: 'index.php?route=common/filemanager&token=<?php echo $token; ?>',
	filebrowserImageUploadUrl: 'index.php?route=common/filemanager&token=<?php echo $token; ?>',
	filebrowserFlashUploadUrl: 'index.php?route=common/filemanager&token=<?php echo $token; ?>'
});
<?php } ?>
//--></script> 
<script type="text/javascript"><!--
function image_upload(field, folder, thumb) {
	$('#dialog').remove();
	
	$('#content').prepend('<div id="dialog" style="padding: 3px 0px 0px 0px;"><iframe src="index.php?route=common/filemanager&token=<?php echo $token; ?>&restaurant_id=<?php echo isset($this->request->get['restaurant_id'])?$this->request->get['restaurant_id']:$this->restaurant->getRestaurantId();?>&folder=' + encodeURIComponent(folder) + '&field=' + encodeURIComponent(field) + '" style="padding:0; margin: 0; display: block; width: 100%; height: 100%;" frameborder="no" scrolling="auto"></iframe></div>');
	
	$('#dialog').dialog({
		title: '<?php echo $text_image_manager; ?>',
		close: function (event, ui) {
			if ($('#' + field).attr('value')) {
				$.ajax({
					url: 'index.php?route=common/filemanager/image&token=<?php echo $token; ?>&image=' + encodeURIComponent($('#' + field).val()),
					dataType: 'text',
					success: function(data) {
						$('#' + thumb).replaceWith('<img src="' + data + '" alt="" id="' + thumb + '" />');
					}
				});
			}
		},
		bgiframe: false,
		width: 800,
		height: 400,
		resizable: false,
		modal: false
	});
};
//--></script> 
<script type="text/javascript"><!--
var attribute_row = <?php echo $attribute_row; ?>;

function addAttribute() {
	html  = '<tbody id="attribute-row' + attribute_row + '">';
    html += '  <tr>';
	html += '    <td class="left"><input size="50" type="text" name="food_attribute[' + attribute_row + '][name]" value="" /><input type="hidden" name="food_attribute[' + attribute_row + '][attribute_id]" value="" /></td>';
	html += '    <td class="left">';
	<?php foreach ($languages as $language) { ?>
	html += '<textarea name="food_attribute[' + attribute_row + '][food_attribute_description][<?php echo $language['language_id']; ?>][text]" cols="40" rows="5"></textarea><img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>" /><br />';
    <?php } ?>
	html += '    </td>';
	html += '    <td class="center"><a onclick="$(\'#attribute-row' + attribute_row + '\').remove();" class="button"><?php echo $button_remove; ?></a></td>';
    html += '  </tr>';	
    html += '</tbody>';
	
	$('#attribute tfoot').before(html);
	
	attributeautocomplete(attribute_row);
	
	attribute_row++;
}

$.widget('custom.catcomplete', $.ui.autocomplete, {
	_renderMenu: function(ul, items) {
		var self = this, currentCategory = '';
		
		$.each(items, function(index, item) {
			if (item.menu != currentCategory) {
				ul.append('<li class="ui-autocomplete-menu">' + item.menu + '</li>');
				
				currentCategory = item.menu;
			}
			
			self._renderItem(ul, item);
		});
	}
});

function attributeautocomplete(attribute_row) {
	$('input[name=\'food_attribute[' + attribute_row + '][name]\']').catcomplete({
		delay: 0,
		source: function(request, response) {
			$.ajax({
				url: 'index.php?route=menu/autocomplete/attribute&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request.term),
				dataType: 'json',
				success: function(json) {
					response($.map(json, function(item) {
						return {
							menu: item.attribute_group,
							label: item.name,
							value: item.attribute_id
						};
					}));
				}
			});
		}, 
		select: function(event, ui) {
			$('input[name=\'food_attribute[' + attribute_row + '][name]\']').attr('value', ui.item.label);
			$('input[name=\'food_attribute[' + attribute_row + '][attribute_id]\']').attr('value', ui.item.value);
			
			return false;
		}
	});
}

$('#attribute tbody').each(function(index, element) {
	attributeautocomplete(index);
});
//--></script> 
<script type="text/javascript"><!--	
var option_row = <?php echo $option_row; ?>;

$('select[name=\'option\']').change(function(){
	var value = $(this).val();
	var arr_value = value.split("_");
	
	$('#selected_hidden_food_option_name').val($('select[name=\'option\'] option:selected').html());
	$('#selected_hidden_food_option_id').val(arr_value[0]);
	$('#selected_hidden_food_option_type').val(arr_value[1]);
});

$('#button-add-option').click(function(){
	if($('select[name=\'option\'] option:selected').val() == ''){
		return false;
	}
	
	var option_name = $('#selected_hidden_food_option_name').val();
	var option_id = $('#selected_hidden_food_option_id').val();
	var option_type = $('#selected_hidden_food_option_type').val();
	
	html  = '<div id="tab-option-' + option_row + '" class="vtabs-content">';
	html += '	<input type="hidden" name="food_option[' + option_row + '][food_option_id]" value="" />';
	html += '	<input type="hidden" name="food_option[' + option_row + '][name]" value="' + option_name + '" />';
	html += '	<input type="hidden" name="food_option[' + option_row + '][option_id]" value="' + option_id + '" />';
	html += '	<input type="hidden" name="food_option[' + option_row + '][type]" value="' + option_type + '" />';
	html += '	<table class="form">';
	html += '	  <tr>';
	html += '		<td><?php echo $entry_required; ?></td>';
	html += '       <td><select name="food_option[' + option_row + '][required]">';
	html += '	      <option value="1"><?php echo $text_yes; ?></option>';
	html += '	      <option value="0"><?php echo $text_no; ?></option>';
	html += '	    </select></td>';
	html += '     </tr>';
	
	if (option_type == 'text') {
		html += '     <tr>';
		html += '       <td><?php echo $entry_option_value; ?></td>';
		html += '       <td><input type="text" name="food_option[' + option_row + '][option_value]" value="" /></td>';
		html += '     </tr>';
	}
	
	if (option_type == 'textarea') {
		html += '     <tr>';
		html += '       <td><?php echo $entry_option_value; ?></td>';
		html += '       <td><textarea name="food_option[' + option_row + '][option_value]" cols="40" rows="5"></textarea></td>';
		html += '     </tr>';						
	}
	 
	if (option_type == 'file') {
		html += '     <tr style="display: none;">';
		html += '       <td><?php echo $entry_option_value; ?></td>';
		html += '       <td><input type="text" name="food_option[' + option_row + '][option_value]" value="" /></td>';
		html += '     </tr>';			
	}
					
	if (option_type == 'date') {
		html += '     <tr>';
		html += '       <td><?php echo $entry_option_value; ?></td>';
		html += '       <td><input type="text" name="food_option[' + option_row + '][option_value]" value="" class="date" /></td>';
		html += '     </tr>';			
	}
	
	if (option_type == 'datetime') {
		html += '     <tr>';
		html += '       <td><?php echo $entry_option_value; ?></td>';
		html += '       <td><input type="text" name="food_option[' + option_row + '][option_value]" value="" class="datetime" /></td>';
		html += '     </tr>';			
	}
	
	if (option_type == 'time') {
		html += '     <tr>';
		html += '       <td><?php echo $entry_option_value; ?></td>';
		html += '       <td><input type="text" name="food_option[' + option_row + '][option_value]" value="" class="time" /></td>';
		html += '     </tr>';			
	}
	
	html += '  </table>';
		
	if (option_type == 'select' || option_type == 'radio' || option_type == 'checkbox' || option_type == 'image') {
		html += '  <table id="option-value' + option_row + '" class="list">';
		html += '  	 <thead>'; 
		html += '      <tr>';
		html += '        <td class="left"><?php echo $entry_option_value; ?></td>';
		html += '        <td class="right" style="width:200px"><?php echo $entry_price; ?></td>';
		html += '        <td style=\'width:100px\'></td>';
		html += '      </tr>';
		html += '  	 </thead>';
		html += '    <tfoot>';
		html += '      <tr>';
		html += '        <td colspan="2"></td>';
		html += '        <td class="center"><a onclick="addOptionValue(' + option_row + ');" class="button"><?php echo $button_add_option_value; ?></a></td>';
		html += '      </tr>';
		html += '    </tfoot>';
		html += '  </table>';
		html += '</div>';	
	}
	
	$('#tab-option').append(html);
	
	$('#option-add').before('<a href="#tab-option-' + option_row + '" id="option-' + option_row + '">' + option_name + '&nbsp;<img src="view/image/delete.png" alt="" onclick="$(\'#vtab-option a:first\').trigger(\'click\'); $(\'#option-' + option_row + '\').remove(); $(\'#tab-option-' + option_row + '\').remove(); return false;" /></a>');
	
	$('#vtab-option a').tabs();
	
	$('#option-' + option_row).trigger('click');		

	if($('.date').length > 0){
		$('.date').datepicker({dateFormat: 'yy-mm-dd'});
	}
	
	if($('.datetime').length > 0){
		$('.datetime').datetimepicker({
			dateFormat: 'yy-mm-dd',
			timeFormat: 'h:m'
		});	
	}

	if($('.time').length > 0){
		$('.time').timepicker({timeFormat: 'h:m'});	
	}
			
	option_row++;
});
//--></script> 
<script type="text/javascript"><!--		
var option_value_row = <?php echo $option_value_row; ?>;

function addOptionValue(option_row) {	
	html  = '<tbody id="option-value-row' + option_value_row + '">';
	html += '  <tr>';
	html += '    <td class="left"><select name="food_option[' + option_row + '][food_option_value][' + option_value_row + '][option_value_id]"></select><input type="hidden" name="food_option[' + option_row + '][food_option_value][' + option_value_row + '][food_option_value_id]" value="" /></td>';
	html += '    <td class="right"><select name="food_option[' + option_row + '][food_option_value][' + option_value_row + '][price_prefix]">';
	html += '      <option value="+">+</option>';
	html += '      <option value="-">-</option>';
	html += '    </select>';
	html += '    <input type="text" name="food_option[' + option_row + '][food_option_value][' + option_value_row + '][price]" value="" size="5" /></td>';
	html += '    <td class="center"><a onclick="$(\'#option-value-row' + option_value_row + '\').remove();" class="button"><?php echo $button_remove; ?></a></td>';
	html += '  </tr>';
	html += '</tbody>';
	
	$('#option-value' + option_row + ' tfoot').before(html);

	$('select[name=\'food_option[' + option_row + '][food_option_value][' + option_value_row + '][option_value_id]\']').load('index.php?route=menu/autocomplete/foodOptionValue&token=<?php echo $token; ?>&option_id=' + $('input[name=\'food_option[' + option_row + '][option_id]\']').attr('value'));
	
	option_value_row++;
}
//--></script> 
<script type="text/javascript"><!--
var special_row = <?php echo $special_row; ?>;

function addSpecial() {
	html  = '<tbody id="special-row' + special_row + '">';
	html += '  <tr>'; 
    html += '    <td class="right"><input type="text" name="food_special[' + special_row + '][priority]" value="" size="2" /></td>';
	html += '    <td class="right"><input type="text" name="food_special[' + special_row + '][price]" value="" /></td>';
    html += '    <td class="left"><input type="text" name="food_special[' + special_row + '][date_start]" value="" class="date" /></td>';
	html += '    <td class="left"><input type="text" name="food_special[' + special_row + '][date_end]" value="" class="date" /></td>';
	html += '    <td class="center"><a onclick="$(\'#special-row' + special_row + '\').remove();" class="button"><?php echo $button_remove; ?></a></td>';
	html += '  </tr>';
    html += '</tbody>';
	
	$('#special tfoot').before(html);
 
	$('#special-row' + special_row + ' .date').datepicker({dateFormat: 'dd/mm/yy'});
	
	special_row++;
}
//--></script> 
<script type="text/javascript"><!--
var image_row = <?php echo $image_row; ?>;

function addImage() {
    html  = '<tbody id="image-row' + image_row + '">';
	html += '  <tr>';
	html += '    <td class="left"><div class="image"><img src="<?php echo $no_image; ?>" alt="" id="thumb' + image_row + '" /><input type="hidden" name="food_image[' + image_row + '][image]" value="" id="image' + image_row + '" /><br /><a onclick="image_upload(\'image' + image_row + '\',\'food/food\', \'thumb' + image_row + '\');"><?php echo $text_browse; ?></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a onclick="$(\'#thumb' + image_row + '\').attr(\'src\', \'<?php echo $no_image; ?>\'); $(\'#image' + image_row + '\').attr(\'value\', \'\');"><?php echo $text_clear; ?></a></div></td>';
	html += '    <td class="right"><input type="text" name="food_image[' + image_row + '][sort_order]" value="" /></td>';
	html += '    <td class="center"><a onclick="$(\'#image-row' + image_row  + '\').remove();" class="button"><?php echo $button_remove; ?></a></td>';
	html += '  </tr>';
	html += '</tbody>';
	
	$('#images tfoot').before(html);
	
	image_row++;
}
//--></script> 
<script type="text/javascript" src="view/javascript/jquery/ui/jquery-ui-timepicker-addon.js"></script> 
<script type="text/javascript"><!--
$('.date').datepicker({dateFormat: 'dd/mm/yy'});
$('.datetime').datetimepicker({
	dateFormat: 'dd/mm/yy',
	timeFormat: 'h:m'
});
$('.time').timepicker({timeFormat: 'h:m'});
//--></script>
<script type="text/javascript"><!--
$('#tabs a').tabs(); 
$('#languages a').tabs(); 
$('#vtab-option a').tabs();
//--></script> 
<?php echo $footer; ?>